//
//  MBDPersonalInfoViewController.swift
//  MasterBoy
//
//  Created by kyson on 2023/3/28.
//

import Foundation


import UIKit
import YYText
import Toast_Swift
import Alamofire
import SwiftyJSON
import RxCocoa
import RxSwift

class MBDPersonalInfoViewController : MBDBaseViewController {
    
    var tableView: UITableView!
    var viewModel : MBDPersonalInfoViewModel!
    var disposeBag = DisposeBag()

    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "个人信息"
        self.viewModel = MBDPersonalInfoViewModel()
        
        tableView = UITableView()
        tableView.translatesAutoresizingMaskIntoConstraints = false
        self.view.addSubview(tableView)
        
        NSLayoutConstraint.activate([
        self.view.safeAreaLayoutGuide.topAnchor.constraint(equalTo: tableView.topAnchor),
                    self.view.bottomAnchor.constraint(equalTo: tableView.bottomAnchor),
                    self.view.leadingAnchor.constraint(equalTo: tableView.leadingAnchor),
                    self.view.trailingAnchor.constraint(equalTo: tableView.trailingAnchor),
                ])

        tableView.contentInset = UIEdgeInsets(top: 20, left: 0, bottom: 0, right: 0)
        tableView.showsVerticalScrollIndicator = false
        tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
        
        tableView.dataSource = self
        tableView.delegate = self
        tableView.estimatedRowHeight = 100

        tableView.register(MBDDetailAvatarCell.self, forCellReuseIdentifier: "MBDDetailAvatarCell")
        tableView.register(MBDDetailUserNameCell.self, forCellReuseIdentifier: "MBDDetailUserNameCell")

        self.userInfoUpdated()
        
        NotificationCenter.default.addObserver(self, selector: #selector(userInfoUpdated), name: NSNotification.Name(rawValue: "UserInfoUpdated"), object: nil)
        
        self.view.setDynamicBackgroundColor()
        tableView.setDynamicBackgroundColor()

    }
    
    @objc func userInfoUpdated(){
        self.viewModel.cellModels = MBDPersonalInfoViewModel.defaultCellModels(accountinfo: MBDAccountInfoModel.current())
        
        self.tableView.reloadData()
    }
    
    override func updateColors() {
        self.view.setDynamicBackgroundColor()
        tableView.setDynamicBackgroundColor()
        self.tableView.reloadData()
    }
    
    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .default
    }
    
    override var prefersStatusBarHidden: Bool {
        return false
    }
    
}


extension MBDPersonalInfoViewController: UITableViewDataSource {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return self.viewModel.cellModels?.count ?? 0
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cellModel = self.viewModel.cellModels![indexPath.row]
        
        if cellModel is MBDDetailAvatarCellModel {
            let realCellModel = cellModel as! MBDDetailAvatarCellModel
            let cell = tableView.dequeueReusableCell(withIdentifier: "MBDDetailAvatarCell", for: indexPath) as! MBDDetailAvatarCell
            cell.cellModel = realCellModel
            return cell
        }
        
        if cellModel is MBDDetailUserNameCellModel {
            let realCellModel = cellModel as! MBDDetailUserNameCellModel
            let cell = tableView.dequeueReusableCell(withIdentifier: "MBDDetailUserNameCell", for: indexPath) as! MBDDetailUserNameCell
            cell.cellModel = realCellModel
            return cell
        }
        
        if cellModel is MBDDetailWechatNumCellModel {
            let realCellModel = cellModel as! MBDDetailWechatNumCellModel
            let cell = tableView.dequeueReusableCell(withIdentifier: "MBDDetailUserNameCell", for: indexPath) as! MBDDetailUserNameCell
            cell.cellModel = realCellModel
            return cell
        }
        
        
        
        return UITableViewCell()
    }
}


extension MBDPersonalInfoViewController : UITableViewDelegate {
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let cellModel = self.viewModel.cellModels![indexPath.row]
        
        if cellModel is MBDDetailUserNameCellModel {
            let text = cellModel.isEmpty() ? "" : cellModel.detailText!
            
            let modifyType : MBDModyfiyInfoType = .nickName
            
            let viewModel = MBDModifyUserInfoViewModel(placeHolder: text,title: cellModel.modifyVCTitle!,modifyInfoType: modifyType)
            let viewController = MBDModifyUserInfoViewController(viewModel: viewModel)

            self.navigationController?.pushViewController(viewController, animated: true)
        }
        
        if  cellModel is MBDDetailWechatNumCellModel {
            let text = cellModel.isEmpty() ? "" : cellModel.detailText!
            
            let viewModel = MBDModifyUserWeChatViewModel(placeHolder: text,title: cellModel.modifyVCTitle!)
            let viewController = MBDModifyUserWeChatViewController(viewModel: viewModel)

            self.navigationController?.pushViewController(viewController, animated: true)
        }
        
         if cellModel is MBDDetailAvatarCellModel {
            //修改图片
            let apvc = MBDAvatarPickerViewController()
            apvc.detailImageURL = cellModel.detailImageURLString
            self.navigationController?.pushViewController(apvc, animated: true)
        }
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let cellModel = self.viewModel.cellModels![indexPath.row] as MBDDetailCellModel

        return cellModel.cellHeight()
    }
}
